﻿using Jhecis.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Jhecis.Handle
{
    public partial class SendForm : Form
    {
        public SendForm()
        {
            InitializeComponent();
            button3.Enabled = false;
        }
        SendHelper SendHelper = new SendHelper();
        BingLiBLL bll = new BingLiBLL();
        private void button1_Click(object sender, EventArgs e)
        {
            string patientid = this.textBox1.Text.Trim();
            button1.Enabled = false;
            DataTable DtAllPatient = bll.GetAllPatientByPatientid(patientid);
            LogHelper.Instance.WriteInfo("时间段上报开始..");
            if (DtAllPatient != null && DtAllPatient.Rows.Count > 0)
            {
                for (int i = 0; i < DtAllPatient.Rows.Count; i++)
                {
                    string pvid = DtAllPatient.Rows[i]["PATIENT_ID"].ToString();
                    SendHelper.SendXmlByPvid(pvid, dateTimePicker1.Value.ToString("yyyyMMdd"));
                }
            }
            LogHelper.Instance.WriteInfo("单人上报完成..");
            button1.Enabled = true;
        }
        private Thread ThMQ = null;
        bool IsContinue = true;
        private void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            button3.Enabled = true;
            ThMQ = new Thread(SendPatientsXml);
            ThMQ.IsBackground = true;
            ThMQ.Start();
        }
        public void SendPatientsXml()
        {
            string start = dateTimePicker1.Value.ToString("yyyy-MM-dd 00:00:00");
            string end = dateTimePicker1.Value.ToString("yyyy-MM-dd 23:59:59");
            DataTable DtAllPatient = bll.GetAllPatients(start, end);
            LogHelper.Instance.WriteInfo("时间段上报开始..");
            if (DtAllPatient != null && DtAllPatient.Rows.Count > 0)
            {
                for (int i = 0; i < DtAllPatient.Rows.Count; i++)
                {
                    string pvid = DtAllPatient.Rows[i]["PATIENT_ID"].ToString();
                    SendHelper.SendXmlByPvid(pvid, dateTimePicker1.Value.ToString("yyyyMMdd"));
                }
            }
            LogHelper.Instance.WriteInfo("时间段上报完成..");
            #region 发送完毕
            SendCallBackDele callBack = new SendCallBackDele(ResetWindow);
            callBack.BeginInvoke(null, null);
            #endregion
        }

        #region 回调
        delegate void SendCallBackDele();
        public void ResetWindow()
        {
            button2.Enabled = true;
            button3.Enabled = false;
            MessageBox.Show("上报完成！");
        }
        #endregion

        private void button3_Click(object sender, EventArgs e)
        {
            IsContinue = false;
            if (ThMQ != null)
            {
                ThMQ.Abort();
            }
        }
    }
}
